package com.apollow.oschina.base;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.apollow.oschina.R;
import com.apollow.oschina.adapter.ViewPageFragmentAdapter;
import com.apollow.oschina.ui.empty.EmptyLayout;

/**
 * Created by bobo on 2017/1/13.
 * 导航基类
 */

public abstract class BaseViewPagerFragment extends BaseFragment {
    protected TabLayout mTabLayout; // ViewPager顶部的导航条
    protected ViewPager mViewPager; // 展示内容用的滚动布局ViewPager
    protected ViewPageFragmentAdapter mTabsAdapter; // 封装了数据集合的ViewPager适配器
    protected EmptyLayout mErrorLayout; // 布局加载异常时, 显示的空布局.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.base_viewpage_fragment, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // 导航条, 可以跟随ViewPapger左右滑动.可以设置自定义的导航条内容
        mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout);

        // 可以滑动的ViewPager
        mViewPager = (ViewPager) view.findViewById(R.id.pager);

        // 空布局
        mErrorLayout = (EmptyLayout) view.findViewById(R.id.error_layout);

        // 封装adapter, 注意这里是继承的FragmentStatePagerAdapter, 并且传入的是getChildFragmentManager()
        // 此处封装了PagerSlidingTabStrip, ViewPager, 在Adapter内部进行一系列的初始化.
        mTabsAdapter = new ViewPageFragmentAdapter(getChildFragmentManager(), mTabLayout, mViewPager);

        setScreenPageLimit();

        onSetupTabAdapter(mTabsAdapter);

    }

    protected void setScreenPageLimit() {

    }

    protected abstract void onSetupTabAdapter(ViewPageFragmentAdapter adapter);
}
